package code.tools.commponents
{
	import code.tools.commponents.button.TextButton;
	
	import flash.utils.Dictionary;

	public class AdaptBtnGroup
	{
		public static var adaptBtnGroup:AdaptBtnGroup = new AdaptBtnGroup();
		private var _adaptSizeDic:Dictionary;
		private var _list:Vector.<TextButton>;
		
		public function AdaptBtnGroup()
		{
			_list = new Vector.<TextButton>();
			_adaptSizeDic = new Dictionary();
		}
		
		/**
		 * 添加新的自适应文本框 
		 * @param tf
		 * 
		 */		
		public function appendTf(tf:TextButton):AdaptBtnGroup
		{
			tf.updateTextCallBack(tfZoomHandler);
			_adaptSizeDic[tf] = tf.getTextSize()
			_list.push(tf);
			return this;
		}
		
		/**
		 * 移除自适应文本框 
		 * @param tf
		 * 
		 */		
		public function removeTf(tf:TextButton):void
		{
			if(_list.indexOf(tf)!=-1)
			{
				tf.updateTextCallBack(null);
				delete _adaptSizeDic[tf];
				_list.splice(_list.indexOf(tf),1);
			}
		}
		
		private function tfZoomHandler(tf:TextButton,size:int):void
		{
			_adaptSizeDic[tf] = size;
			var minSize:int = size;
			for each(var value:int in _adaptSizeDic)
			{
				if(minSize > value)
					minSize = value;
			}
			synchroTextSize(minSize);
		}
		
		/**
		 * 组内的文本框同步尺寸 
		 * 
		 */		
		private function synchroTextSize(size:int):void
		{
			for(var i:int = 0;i < _list.length;i++)
				_list[i].updateTextSize(size);
		}
	}
}